package com.kiln.juxingqs.ui.vm

import android.app.Application
import android.net.Uri
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import client.global.commlib.network.data.JsonResult
import client.global.commlib.network.data.Ret
import client.global.commlib.out_time
import client.global.commlib.toast.toast
import client.global.commlib.utils.Util
import com.kiln.juxingqs.api.bean.UploadRet
import com.kiln.juxingqs.api.bean.UserInfo
import com.kiln.juxingqs.ui.viewmodel.MiddleVm
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Response

class SetRealViewModel(app: Application) : MiddleVm(app) {
    val call = MutableLiveData<JsonResult<Ret>>()
    val isShowing = MutableLiveData<Boolean>()
    val realname = MutableLiveData<String>("")
    val fontUrl = MutableLiveData<String>("")
    val idcard = MutableLiveData<String>("")
    val idFontImg = MutableLiveData<Uri>()

    val userInfo = MutableLiveData<UserInfo>()
    val sex = MutableLiveData<Int>(-1)

    fun sexChange(vale: Int) {
        sex.value = vale
    }

    fun ok(lo: LifecycleOwner) {
        isShowing.value = true
        viewModelScope.launch {
            flow {
                var emm: Response<JsonResult<UploadRet>>? = null
                if (fontUrl.value.isNullOrEmpty()) {
                    val file = Util.getLuBan(
                        lo,
                        idFontImg.value!!
                    )
                    val requestFile: RequestBody = RequestBody.create(
                        "multipart/form-data".toMediaTypeOrNull(),
                        file
                    )
                    val body = MultipartBody.Part.createFormData(
                        "file",
                        file.name,
                        requestFile
                    )
                    val descriptionString = "file"
                    val description = RequestBody.create(
                        "multipart/form-data".toMediaTypeOrNull(),
                        descriptionString
                    )
                    out_time {
                        val ret = server.upload(description, body)
                        emm = ret
                    }
                }
                emit(emm)
            }.map {
                var emm: Response<JsonResult<Ret>>? = null

                it?.body()?.let {

                    if (it.isSuccess) {
                        out_time {
                            val ret = server.set_real(
                                idcard.value!!,
                                it.data!!.url,
                                realname.value!!,
                                "${sex.value!!}"
                            )
                            emm = ret
                        }
                    }
                }
                emm
            }.flowOn(Dispatchers.IO)
                .onCompletion {
                    isShowing.value = false
                }.catch { cause ->
                    if (cause != null) {
                        toast(cause.message)
                    }
                }.collectLatest {
                    call.value = it?.body()

                }
        }
    }

}